using Avalonia.Controls;
using Avalonia.Interactivity;
using CommunityToolkit.Mvvm.Messaging;
using DialogHostAvalonia;
using Microsoft.Extensions.DependencyInjection;
using SchoolApp_Material.Msgs;
using SchoolApp_Material.ViewModels;

namespace SchoolApp_Material.Views;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = Program.ServiceProvider.GetRequiredService<MainViewModel>();
    }

    private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
    {
        WeakReferenceMessenger.Default.Register<ErrorMsg>(this, async (r, msg) =>
        {
            var result = await DialogHost.Show(
                new MsgBoxView
                {
                    DataContext = new MsgBoxViewModel()
                    {
                        MsgBoxIcon = MsgBoxIcons.Error, 
                        Title = "错误", 
                        Content = msg.Data,
                        MsgBoxShowButton = MsgBoxShowButtons.Ok,
                        DefaultEscape = MsgBoxButtons.Ok,
                        DefaultEnter = MsgBoxButtons.Ok
                    }
                },
                "LoginWindowHost");
            if (result is MsgBoxButtons msgBoxButton)
            {

            }
        });
    }

    private void MainWindow_OnUnloaded(object sender, RoutedEventArgs e)
    {
        WeakReferenceMessenger.Default.Unregister<ErrorMsg>(this);
    }
}